package net.lzzy.practices.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Getter
@Setter
public class Result  extends  BaseEntity{
    /**
     * 0代表正确
     * 1代表少选
     * 2代表多选
     * 3代表错选
     * 4代表未做
     */
    @Transient
    public static final int TYPE_RIGHT=0;
    @Transient
    public static final int TYPE_MISS=1;
    @Transient
    public static final int TYPE_EXTRA=2;
    @Transient
    public static final int TYPE_WRONG=3;
    @Transient
    public static final int TYPE_UNFINISHED=4;

    @Temporal(TemporalType.TIMESTAMP)
    private Date commitTime;

    @ManyToOne
    private User student;

    @ManyToOne
    private Question question;

    @ManyToMany
    private List<Option> options=new ArrayList<>();

    public Result(){
        commitTime=new Date();
    }
    @JsonBackReference
    public User getStudent() {
        return student;
    }
    @JsonBackReference
    public Question getQuestion() {
        return question;
    }
    @JsonIgnore
    public List<Option> getOptions() {
        return options;
    }
    public int what() {
        if (options.size() == 0) {
            return TYPE_UNFINISHED;
        }
        boolean miss = false;
        boolean extra = false;
        for (Option option : options) {
            if (!option.isAnswer()) {
                extra = true;
                break;
            }
        }
        for (Option option : question.getOptions()) {
            if (option.isAnswer()) {
                if (!options.contains(option)) {
                    miss = true;
                    break;
                }
            }
        }
        if (miss && extra) {
            return TYPE_WRONG;
        } else if (miss) {
            return TYPE_MISS;
        } else if (extra) {
            return TYPE_EXTRA;
        } else {
            return TYPE_RIGHT;
        }
    }

}
